//// MealOrderReminderServiceImpl.java
//package com.ruoyi.system.service.impl;
//
//import com.ruoyi.system.service.IMailService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.mail.SimpleMailMessage;
//import org.springframework.mail.javamail.JavaMailSender;
//import org.springframework.stereotype.Service;
//
///**
// * 订餐提醒服务实现类
// */
//@Service
//public class MealOrderReminderServiceImpl implements IMailService {
//
//    private static final Logger log = LoggerFactory.getLogger(MealOrderReminderServiceImpl.class);
//
//    @Autowired
//    private JavaMailSender mailSender;
//
//    @Value("${spring.mail.username}")
//    private String from;
//
//    /**
//     * 发送自定义邮件
//     */
//    public void sendUnfilledMealOrderReminders(String to, String subject, String content) {
//        log.info("开始发送自定义邮件");
//        try {
//            sendSimpleMail(to, subject, content);
//            log.info("自定义邮件发送成功: to={}", to);
//        } catch (Exception e) {
//            log.error("自定义邮件发送失败: to={}", to, e);
//            throw new RuntimeException("邮件发送失败", e);
//        }
//    }
//
//    @Override
//    public void sendSimpleMail(String to, String subject, String content) {
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(from);
//        message.setTo(to);
//        message.setSubject(subject);
//        message.setText(content);
//        mailSender.send(message);
//        log.info("简单邮件发送成功: to={}, subject={}", to, subject);
//    }
//
//    @Override
//    public void sendHtmlMail(String to, String subject, String content) {
//        // 对于订餐提醒业务，暂时不需要HTML邮件功能
//        // 如果将来需要，可以实现该方法
//        sendSimpleMail(to, subject, content);
//        log.info("HTML邮件发送成功: to={}, subject={}", to, subject);
//    }
//}
// MealOrderReminderServiceImpl.java
package com.ruoyi.system.service.impl;

import com.ruoyi.system.service.IMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;

/**
 * 订餐提醒服务实现类
 */
@Service
public class MealOrderReminderServiceImpl implements IMailService {

    private static final Logger log = LoggerFactory.getLogger(MealOrderReminderServiceImpl.class);

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送自定义邮件
     */
    public void sendUnfilledMealOrderReminders(String to, String subject, String content) {
        log.info("开始发送自定义邮件");
        try {
            sendSimpleMail(to, subject, content);
            log.info("自定义邮件发送成功: to={}", to);
        } catch (Exception e) {
            log.error("自定义邮件发送失败: to={}", to, e);
            throw new RuntimeException("邮件发送失败", e);
        }
    }

    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        // 处理多个收件人，支持逗号或分号分隔
        String[] recipients = parseRecipients(to);

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(recipients);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
        log.info("简单邮件发送成功: to={}, subject={}", Arrays.toString(recipients), subject);
    }

    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        // 对于订餐提醒业务，暂时不需要HTML邮件功能
        // 如果将来需要，可以实现该方法
        sendSimpleMail(to, subject, content);
        log.info("HTML邮件发送成功: to={}, subject={}", to, subject);
    }

    /**
     * 解析收件人列表，支持逗号或分号分隔的多个邮箱地址
     * @param to 收件人字符串
     * @return 收件人数组
     */
    private String[] parseRecipients(String to) {
        if (!StringUtils.hasText(to)) {
            throw new IllegalArgumentException("收件人地址不能为空");
        }

        // 使用逗号或分号分隔多个邮箱地址，并去除空格
        return Arrays.stream(to.split("[,;]"))
                .map(String::trim)
                .filter(StringUtils::hasText)
                .toArray(String[]::new);
    }
}
